package com.ruoyi.utils;

import java.util.Iterator;

//实现多级评论，将此篇文章的全部评论封装成为一棵树


    /**
     *
     * 迭代器
     *
     * @author
     * @create
     **/
    public class TreeNodeIterator<T> implements Iterator<TreeNode<T>> {

        enum ProcessStages {
            ProcessParent, ProcessChildCurNode, ProcessChildSubNode
        }

        private ProcessStages doNext;

        private TreeNode<T> next;

        private Iterator<TreeNode<T>> childrenCurNodeIter;

        private Iterator<TreeNode<T>> childrenSubNodeIter;

        private TreeNode<T> treeNode;

        public TreeNodeIterator(TreeNode<T> treeNode) {
            this.treeNode = treeNode;
            this.doNext = ProcessStages.ProcessParent;
            this.childrenCurNodeIter = treeNode.children.iterator();
        }

        @Override
        public boolean hasNext() {

            if (this.doNext == ProcessStages.ProcessParent) {
                this.next = this.treeNode;
                this.doNext = ProcessStages.ProcessChildCurNode;
                return true;
            }

            if (this.doNext == ProcessStages.ProcessChildCurNode) {
                if (childrenCurNodeIter.hasNext()) {
                    TreeNode<T> childDirect = childrenCurNodeIter.next();
                    childrenSubNodeIter = childDirect.iterator();
                    this.doNext = ProcessStages.ProcessChildSubNode;
                    return hasNext();
                } else {
                    this.doNext = null;
                    return false;
                }
            }

            if (this.doNext == ProcessStages.ProcessChildSubNode) {
                if (childrenSubNodeIter.hasNext()) {
                    this.next = childrenSubNodeIter.next();
                    return true;
                } else {
                    this.next = null;
                    this.doNext = ProcessStages.ProcessChildCurNode;
                    return hasNext();
                }
            }

            return false;
        }

        @Override
        public TreeNode<T> next() {
            return this.next;
        }

        /**
         * 目前不支持删除节点
         */
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }
//    public  static TreeNode buildTree(List<CommentVo> commentVoList){
//        //创建根节点 id为0
//        TreeNode root = TreeNode.buildRoot();
//        //根节点的子节点列表
//        List<TreeNode> rootChildren=new ArrayList<>();
//        //将评论封装为树的节点
//        List<TreeNode> nodes=new ArrayList<>();
//        for (CommentVo commentVo : commentVoList) {
//            TreeNode TreeNode = com.library.utils.TreeNode.buildNode(commentVo);
//            nodes.add(TreeNode);
//        }
//        for (TreeNode node : nodes) {
//            TreeNode parentNode=findParentNode(node,nodes);
//            if(parentNode!=null){
//                //不为空，说明有父节点
//                if(parentNode.getChildren()==null){
//                    //说明这个节点是父节点的第一个子节点，创建一个子节点数组
//                    List<TreeNode> children=new ArrayList<>();
//                    children.add(node);
//                    parentNode.setChildren(children);
//                }else{
//                    parentNode.getChildren().add(node);
//                }
//            }else{
//                //为空，说明父节点为根节点
//                //父节点添加子节点
//                rootChildren.add(node);
//                //子节点添加父节点id
//                node.setParentId(root.getId());
//            }
//            root.setChildren(rootChildren);
//        }
//
//        return root;
//    }
//    //找到子节点的父节点
//    public static TreeNode findParentNode(TreeNode childNode,List<TreeNode> allNodes){
//        for (TreeNode node : allNodes) {
//            if(childNode.getParentId().equals(node.getId())){
//                //将子评论的内容添加到父评论的children列表中。
//                //如果父评论(CommentVo)的子评论列表为null,则新建一个列表。
//                List<CommentVo> fatherCommentVoList = node.getContent().getChildren();
//                if(fatherCommentVoList==null){
//                    fatherCommentVoList=new ArrayList<>();
//                    fatherCommentVoList.add(childNode.getContent());
//                    node.getContent().setChildren(fatherCommentVoList);
//                }else{
//                    node.getContent().getChildren().add(childNode.getContent());
//                }
//                //添加此节点的父亲节点
//                childNode.setParentId(node.getId());
//                return node;
//            }
//        }
//        return null;
//
//    }



